﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.Windows.Forms;
using System.Configuration;


namespace PassData
{
    [RunInstaller(true)]
    public partial class Installer1 : System.Configuration.Install.Installer
    {
        public Installer1()
        {
            InitializeComponent();
        }
        public override void Install(System.Collections.IDictionary stateSaver)
        {
            string serverName = Context.Parameters["ServerName"];
            string instanceName = Context.Parameters["InstanceName"];
            string databaseName = Context.Parameters["DatabaseName"];
            string message = string.Empty;

            if (serverName == string.Empty) message = "There was no Server Name specified.";
            if (instanceName == string.Empty) message = "There was no Instance Name specified.";
            if (databaseName == string.Empty) message = "There was no Database Name specified";

            if (message != string.Empty)
            {
                MessageBox.Show(message);
                Rollback(stateSaver);
            }
            else base.Install(stateSaver);

            //System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            //string conn = config.AppSettings.Settings["SPIWare3ConnectionString"].Value;

            //string targetDirectory = Context.Parameters["targetdir"]; 
            //string targetDirectory = @"C:\Program Files\Cullen's Creations\SPIWare3\";
            string targetDirectory = StripDir(Context.Parameters["AssemblyPath"]);
            
            string exePath = string.Format("{0}\\SPIWare.exe", targetDirectory);
            Configuration config = ConfigurationManager.OpenExeConfiguration(exePath);

            string conn = config.AppSettings.Settings["SPIWare3ConnectionString"].Value;
            string newConn = conn.Replace("{ServerName}", serverName).Replace("{InstanceName}", instanceName).Replace("{DatabaseName}", databaseName);
            
            config.AppSettings.Settings["SPIWare3ConnectionString"].Value = newConn;
            config.Save();
        }
        private string StripDir(string fullPath)
        {
            string retValue = default(string);
            if (fullPath != null && fullPath != string.Empty && fullPath != default(string))
            {
                retValue = fullPath.Substring(0, fullPath.LastIndexOf(@"\"));
            }
            return retValue;
        }

        public override void Commit(IDictionary savedState)
        {
            base.Commit(savedState);
        }

        public override void Rollback(IDictionary savedState)
        {
            base.Rollback(savedState);
        }

        public override void Uninstall(IDictionary savedState)
        {
            base.Uninstall(savedState);
        }
    }
}
